package mining;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import mining.CsvReader;

public abstract class ItemSetMining {
	
	protected int minFreqItemsetSupport = 0;
	protected int minRareItemsetSupport = 0;
	protected int transactionCount = 0;
	protected int itemCount = 0;
	protected String filePath;
	protected String filename;

	protected ItemSetMining() {
		
	}
	
	protected ItemSetMining(String filePath) {
		this.filePath = filePath;
	}
	
	public void setMinFreqItemsetSupport(int minFreqItemsetSupport) {
		this.minFreqItemsetSupport = minFreqItemsetSupport;
	}

	public int getMinFreqItemsetSupport() {
		return this.minFreqItemsetSupport;
	}

	public void setMinRareItemsetSupport(int minRareItemsetSupport) {
		this.minRareItemsetSupport = minRareItemsetSupport;
	}

	public int getMinRareItemsetSupport() {
		return this.minRareItemsetSupport;
	}

	/**
	 * Finds the frequencies of all items in the transaction database.
	 * The returned vector's indices represents the item names, and the 
	 * value represents their support in the database.
	 */
	protected Vector<Integer> findDbInfo()
	{
		Vector<Integer> items = new Vector<Integer>();
		int transactionCount = 0;
		for (CsvReader reader = new CsvReader(this.filePath); reader.hasNext();)
		{
			List<Integer> transaction = new ArrayList<Integer>(reader.read());
			for (int item : transaction)
			{ // count item frequency
				if (items.size() <= item)
					items.setSize(item + 1);
				if (items.get(item) == null)
				{
					items.set(item, 1);
				} else
				{
					items.set(item, items.get(item) + 1);
				}
			}
			transactionCount++;
		}
		this.transactionCount = transactionCount;
		this.itemCount = items.size();
//		items.trimToSize();
		return items;
//		System.out.println("ItemSetMining transactionCount: " + transactionCount + ", itemCount: " + uniqueItemCount);
	}
	
	// count frequency of each 1-item set
	// count number of transactions in total
	// does not check item supports
	public Map<Integer, Integer> countItemSupport() {
		Map<Integer, Integer> itemFreqs = new HashMap<Integer, Integer>();

		for (CsvReader reader = new CsvReader(this.filePath); reader.hasNext(); ) {	// look through all transactions
			List<Integer> list = reader.read();
			for (int item : list) {					// count item frequency
				if (!itemFreqs.containsKey(item)) {
					itemFreqs.put(item, 1);
				} else {
					itemFreqs.put(item, itemFreqs.get(item)+1);
				}
			}
		}
//		System.out.println("ItemSetMining transactionCount: " + transactionCount + ", itemCount: " + uniqueItemCount);
		return itemFreqs;
	}

	public int getTransactionCount() {
		return this.transactionCount;
	}

	public int getItemCount() {
		return this.itemCount;
	}
	
}
